*** Graded IRT test with balancing to identify 0 as independent

clear

use "data/anes_timeseries_cdf_stata_20220916.dta"

gen year = VCF0004

*** Party ID Items

gen pid7 = VCF0301
replace pid7 = . if pid7==0
** 1952 issue?
replace pid7 = 4 if VCF0305==1

gen strongrep = 0 if pid7!=.
replace strongrep = 1 if pid7==7

gen rep_id = 0 if pid7!=.
replace rep_id = 1 if pid7>=6 & pid7!=.

gen leanrep_id = 0 if pid7!=.
replace leanrep_id = 1 if pid7>=5 & pid7!=.

gen strongdem = 0 if pid7!=.
replace strongdem = 1 if pid7==1 & pid7!=.

gen dem_id = 0 if pid7!=.
replace dem_id = 1 if pid7>=1 & pid7<=2

gen leandem_id = 0 if pid7!=.
replace leandem_id = 1 if pid7>=1 & pid7<=3

gen lean3 = 0 if VCF0301==3
replace lean3 = 1 if VCF0301==4
replace lean3 = 2 if VCF0301==5

gen partyid_base = 0 if VCF0303==1
replace partyid_base = 1 if VCF0303==2
replace partyid_base = 2 if VCF0303==3
** 1952 issue
replace partyid_base = 1 if VCF0305==1

gen strongpartisan = 1 if partyid_base!=.
replace strongpartisan = 0 if pid7==1
replace strongpartisan = 2 if pid7==7


** Thermometer Items

gen reptherm = VCF0224
replace reptherm = VCF0202 if year<=1976
replace reptherm = . if reptherm==98
replace reptherm = . if reptherm==99

gen demtherm = VCF0218
replace demtherm = VCF0201 if year<=1976
replace demtherm = . if demtherm==98
replace demtherm = . if demtherm==99

gen thermdiff = reptherm-demtherm

gen partythermbase = 0 if thermdiff<0 & thermdiff!=.
replace partythermbase = 1 if thermdiff==0
replace partythermbase = 2 if thermdiff>0 & thermdiff<=97

gen presreptherm = VCF0426
replace presreptherm = . if presreptherm==98
replace presreptherm = . if presreptherm==99

gen presdemtherm = VCF0424
replace presdemtherm = . if presdemtherm==98
replace presdemtherm = . if presdemtherm==99

gen presthermdiff = presreptherm-presdemtherm

gen partypresthermbase = 0 if presthermdiff>0 & presthermdiff!=.
replace partypresthermbase = 1 if presthermdiff==0
replace partypresthermbase = 2 if presthermdiff<0


*** Likes dislikes

gen demlikes = VCF0374
replace demlikes = . if demlikes==9
recode demlikes 1=1 5=0 8=0
replace demlikes = 0 if VCF0314==0 & year<=1968
replace demlikes = 1 if VCF0314>0 & VCF0314<=5 & year<=1968

gen demdislikes = VCF0380
replace demdislikes = . if demdislikes==9
recode demdislikes 1=1 5=0 8=0
replace demdislikes = 0 if VCF0315==0 & year<=1968
replace demdislikes = 1 if VCF0315>0 & VCF0315<=5 & year<=1968


gen replikes = VCF0386
replace replikes = . if replikes==9
recode replikes 1=1 5=0 8=0
replace replikes = 0 if VCF0318==0 & year<=1968
replace replikes = 1 if VCF0318>0 & VCF0318<=5 & year<=1968

gen repdislikes = VCF0392
replace repdislikes = . if repdislikes==9
recode repdislikes 1=1 5=0 8=0
replace repdislikes = 0 if VCF0319==0 & year<=1968
replace repdislikes = 1 if VCF0319>0 & VCF0319<=5 & year<=1968

gen partyaffect = (replikes+demdislikes)- (demlikes+repdislikes) + 2



*** Votes

gen partyvotepres3 = 0 if VCF0706==1
replace partyvotepres3 = 2 if VCF0706==2
replace partyvotepres3 = 1 if VCF0706==3
replace partyvotepres3 = 1 if VCF0706==4

gen partyvotehouse3 = 0 if VCF0736==1
replace partyvotehouse3 = 2 if VCF0736==5
replace partyvotehouse3 = 1 if VCF0736==7

** Presidential Pref (including non-voters)

gen partyprespref =partyvotepres3
replace partyprespref = 0 if VCF9023==1
replace partyprespref = 2 if VCF9023==2
replace partyprespref = 1 if VCF9023==3
replace partyprespref = 1 if VCF9023==7


            
** Lib con

gen libcon_resp = VCF0803
recode libcon_resp 0=. 9=.
gen libcon_dem = VCF0503
recode libcon_dem 0=. 8=.
gen libcon_rep = VCF0504
recode libcon_rep 0=. 8=.

gen partylibcon3 = 0 if abs(libcon_resp-libcon_dem)<abs(libcon_resp-libcon_rep) & libcon_resp!=.
replace partylibcon3 = 1 if abs(libcon_resp-libcon_dem)==abs(libcon_resp-libcon_rep) & libcon_resp!=.
replace partylibcon3 = 2 if abs(libcon_resp-libcon_dem)>abs(libcon_resp-libcon_rep) & libcon_resp!=.

*** These are now all presidential candidate based

** Gov Serv

gen govserv_resp = VCF0839
recode govserv_resp 0=. 9=.
gen govserv_dem = VCF9086
recode govserv_dem 0=. 8=. 9=.
gen govserv_rep = VCF9094
recode govserv_rep 0=. 8=. 9=.

gen partygovserv3 = 0 if abs(govserv_resp-govserv_dem)<abs(govserv_resp-govserv_rep) & govserv_resp!=.
replace partygovserv3 = 1 if abs(govserv_resp-govserv_dem)==abs(govserv_resp-govserv_rep) & govserv_resp!=. & govserv_rep!=. & govserv_dem!=.
replace partygovserv3 = 2 if abs(govserv_resp-govserv_dem)>abs(govserv_resp-govserv_rep) & govserv_resp!=.


gen netgovserv = abs(govserv_resp-govserv_dem)-abs(govserv_resp-govserv_rep) 

*** Abortion

gen abortion_resp = VCF0838
replace abortion_resp = VCF0837 if VCF0838==.
recode abortion_resp 0=. 9=.

gen abortion_dem = VCF9234
recode abortion_dem -9=. -8=.
gen abortion_rep = VCF9235
recode abortion_rep -9=. -8=.

gen netabortion = abs(abortion_resp-abortion_dem)-abs(abortion_resp-abortion_rep) 

** Aid to Blacks

gen aidtoblacks_resp = VCF0830
recode aidtoblacks_resp 0=. 9=.

gen aidtoblacks_dem = VCF9084
recode aidtoblacks_dem 0=. 8=. 9=.
gen aidtoblacks_rep = VCF9092 
recode aidtoblacks_rep 0=. 8=. 9=.

gen netaidtoblacks = abs(aidtoblacks_resp-aidtoblacks_dem)-abs(aidtoblacks_resp-aidtoblacks_rep)

** Health Insurance

gen health_resp = VCF0806
recode aidtoblacks_resp 0=. 9=.

gen health_dem = VCF9085
recode health_dem 0=. 8=. 9=.
gen health_rep = VCF9093 
recode health_rep 0=. 8=. 9=.

gen nethealth = abs(health_resp-health_dem)-abs(health_resp-health_rep)


**********************
** RECODE as >0, for Stata IRT estimates
**********************

gen attpartyid5 = pid7
*recode attpartyid5 1= 0 2/3=1 4=2 5/6=3 7=4
recode attpartyid5 1/2 = 0 3=1 4=2 5=3 6/7=4

gen attpartystrength = 1 if pid7!=.
replace attpartystrength=0 if pid7==1
replace attpartystrength=2 if pid7==7

gen attreptherm = 0 if reptherm<=20
replace attreptherm = 1 if reptherm<=40 & reptherm>20
replace attreptherm = 2 if reptherm<=60 & reptherm>40
replace attreptherm = 3 if reptherm<=80 & reptherm>60
replace attreptherm = 4 if reptherm<=100 & reptherm>80

gen attdemtherm = 0 if demtherm<=20
replace attdemtherm = 1 if demtherm<=40 & demtherm>20
replace attdemtherm = 2 if demtherm<=60 & demtherm>40
replace attdemtherm = 3 if demtherm<=80 & demtherm>60
replace attdemtherm = 4 if demtherm<=100 & demtherm>80

gen attpresreptherm = 0 if presreptherm<=20
replace attpresreptherm = 1 if presreptherm<=40 & presreptherm>20
replace attpresreptherm = 2 if presreptherm<=60 & presreptherm>40
replace attpresreptherm = 3 if presreptherm<=80 & presreptherm>60
replace attpresreptherm = 4 if presreptherm<=100 & presreptherm>80

gen attpresdemtherm = 0 if presdemtherm<=20
replace attpresdemtherm = 1 if presdemtherm<=40 & presdemtherm>20
replace attpresdemtherm = 2 if presdemtherm<=60 & presdemtherm>40
replace attpresdemtherm = 3 if presdemtherm<=80 & presdemtherm>60
replace attpresdemtherm = 4 if presdemtherm<=100 & presdemtherm>80

gen attpartythermdiff = 0 if thermdiff <= -75
replace attpartythermdiff = 1 if thermdiff<=-50 & thermdiff>-75
replace attpartythermdiff = 2 if thermdiff<=-25 & thermdiff>-50
replace attpartythermdiff = 3 if thermdiff<0 & thermdiff>-25
replace attpartythermdiff = 4 if thermdiff==0
replace attpartythermdiff = 5 if thermdiff<=25 & thermdiff>0
replace attpartythermdiff = 6 if thermdiff<=50 & thermdiff>25
replace attpartythermdiff = 7 if thermdiff<=75 & thermdiff>50
replace attpartythermdiff = 8 if thermdiff<=100 & thermdiff>75

gen attpresthermdiff = 0 if presthermdiff <= -75
replace attpresthermdiff = 1 if presthermdiff<=-50 & presthermdiff>-75
replace attpresthermdiff = 2 if presthermdiff<=-25 & presthermdiff>-50
replace attpresthermdiff = 3 if presthermdiff<0 & presthermdiff>-25
replace attpresthermdiff = 4 if presthermdiff==0
replace attpresthermdiff = 5 if presthermdiff<=25 & presthermdiff>0
replace attpresthermdiff = 6 if presthermdiff<=50 & presthermdiff>25
replace attpresthermdiff = 7 if presthermdiff<=75 & presthermdiff>50
replace attpresthermdiff = 8 if presthermdiff<=100 & presthermdiff>75


gen partylibcon= abs(libcon_resp-libcon_dem)-abs(libcon_resp-libcon_rep)
recode partylibcon -6/-2=0 -1=1 0=2 1=3 2/6=4

gen partyabortion = netabortion
recode partyabortion -3/-1=0 0=1 1/3=2

gen partyaidtoblacks = netaidtoblacks
recode partyaidtoblacks -6/-1=0 0=1 1/6=2

gen partyhealth = nethealth
recode partyhealth -6/-1=0 0=1 1/6=2


**Better nation's economy

gen betterecon = 0 if VCF9205==1
replace betterecon = 1 if VCF9205==2
replace betterecon = 1 if VCF9205==7
replace betterecon = 2 if VCF9205==3



**** NOW DEFINE YEARS TO INCLUDE

forval x = 1/18 {
    di 1948+4*`x'
    sum partyid_base attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan if year==(1948+4*`x')
    }

drop if year==1948

***************
** Pre post
gen post = year>=2000

***************

gen compareyears = .
forval x = 1/18 {
    replace compareyears = 1948+4*`x' if year==(1948+4*`x')
}
tab compareyears


** Create variable to modify weights for pre-post test with balanced year weights

gen newweight = VCF0009z

** Ideology is after 1968, abortion is after 1988

************************
*** Demonstrating that GSEM command with constraint is equivalent to Stata's GRM default

irt grm  partyid_base attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan  [pw=newweight], group(post) intpoints(15)

constraint 1 _b[/partyid_base:1.post#c.cut1]+_b[/partyid_base:1.post#c.cut2]=0
constraint 2 _b[/partyid_base:0.post#c.cut1]+_b[/partyid_base:0.post#c.cut2]=0
constraint 3 _b[/partyid_base:0.post#c.cut1]=_b[/partyid_base:1.post#c.cut1]

gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm constraints(1/3)
matrix beta = e(b)

** Same log likelihood

*** WEIGHTS

*** make years weighted evenly within group to account for varying sample sizes by year
tab year post if e(sample)
di 44810.41/24
replace newweight = newweight*1867.1004/1894 if year==1952
replace newweight = newweight*1867.1004/1088 if year==1954
replace newweight = newweight*1867.1004/1762 if year==1956
replace newweight = newweight*1867.1004/1822 if year==1958
replace newweight = newweight*1867.1004/1954 if year==1960
replace newweight = newweight*1867.1004/1240 if year==1962
replace newweight = newweight*1867.1004/1571 if year==1964
replace newweight = newweight*1867.1004/1289 if year==1966
replace newweight = newweight*1867.1004/1557 if year==1968
replace newweight = newweight*1867.1004/1502 if year==1970
replace newweight = newweight*1867.1004/2705 if year==1972
replace newweight = newweight*1867.1004/2512 if year==1974
replace newweight = newweight*1867.1004/2868.5 if year==1976
replace newweight = newweight*1867.1004/2303 if year==1978
replace newweight = newweight*1867.1004/1614 if year==1980
replace newweight = newweight*1867.1004/1418 if year==1982
replace newweight = newweight*1867.1004/2252 if year==1984
replace newweight = newweight*1867.1004/2174 if year==1986
replace newweight = newweight*1867.1004/2040 if year==1988
replace newweight = newweight*1867.1004/1976 if year==1990
replace newweight = newweight*1867.1004/2486.7793 if year==1992
replace newweight = newweight*1867.1004/1794.0959 if year==1994
replace newweight = newweight*1867.1004/1711.4197 if year==1996
replace newweight = newweight*1867.1004/1276.6137 if year==1998
di 25269.4/7
replace newweight = newweight*3609.9143/1807 if year==2000
replace newweight = newweight*3609.9143/1475.5282 if year==2002
replace newweight = newweight*3609.9143/1212.0019 if year==2004
replace newweight = newweight*3609.9143/2321.7302 if year==2008
replace newweight = newweight*3609.9143/5912.1998 if year==2012
replace newweight = newweight*3609.9143/4264.282 if year==2016
replace newweight = newweight*3609.9143/8276.659 if year==2020
replace newweight = newweight * 42099/44810.41 if post==0
replace newweight = newweight * 25281/25269.4 if post==1

sum newweight [iw=newweight] if e(sample) & post==0
sum newweight [iw=newweight] if e(sample) & post==1

****Now constrain a little more

constraint 4 _b[/attpartythermdiff:1.post#c.cut4]+_b[/attpartythermdiff:1.post#c.cut5]=0
constraint 5 _b[/attpartythermdiff:0.post#c.cut4]+_b[/attpartythermdiff:0.post#c.cut5]=0
constraint 6 _b[/attpartythermdiff:0.post#c.cut4]=_b[/attpartythermdiff:1.post#c.cut4]

constraint 7 _b[/partyaffect:1.post#c.cut2]+_b[/partyaffect:1.post#c.cut3]=0
constraint 8 _b[/partyaffect:0.post#c.cut2]+_b[/partyaffect:0.post#c.cut3]=0
constraint 9 _b[/partyaffect:0.post#c.cut2]=_b[/partyaffect:1.post#c.cut2]


** For some reason these estimates blow up when the variances are high and the constraints get changed, reset to something easier to estimate at first 
matrix beta[1,73] = 10
matrix beta[1,74] = 10

***************************************************
*** Fixed Relevance Model, pre-post test for Table 2
***************************************************


gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm constraints(1/9) from(beta)
matrix newbeta =e(b)


*********************************************
** now free it up

** Takes 66 iterations
*gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm constraints(1/9) ginvariant(none)

matrix newbeta[1,73]=10
matrix newbeta[1,74]=10

gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm constraints(1/3 4/5 7/8) ginvariant(none) from(newbeta)

estimates replay, coeflegend
test _b[/var(LPID)#1.post]= _b[/var(LPID)#0.post]

matrix newbeta = e(b)

*** Looser models don't drastically change estimates of polarization in latent partisan attachments, or show less change
matrix newbeta[1,73]=10
matrix newbeta[1,74]=10

gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm constraints(1/3) ginvariant(none) from(newbeta)
test _b[/var(LPID)#1.post]= _b[/var(LPID)#0.post]

test _b[/attpartythermdiff:0.post#c.cut4]+_b[/attpartythermdiff:0.post#c.cut5]=0
test _b[/attpartythermdiff:1.post#c.cut4]+_b[/attpartythermdiff:1.post#c.cut5]=0, accum

test _b[/partyaffect:0.post#c.cut2]+_b[/partyaffect:0.post#c.cut3]=0
test _b[/partyaffect:1.post#c.cut2]+_b[/partyaffect:1.post#c.cut3]=0, accum

matrix newbeta = e(b)
matrix newbeta[1,73]=10
matrix newbeta[1,74]=10

gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm constraints(1/2) ginvariant(none) 
estimates replay, coeflegend
test _b[/var(LPID)#1.post]= _b[/var(LPID)#0.post]
test _b[/partyid_base:1.post#c.cut1]=_b[/partyid_base:0.post#c.cut1]

** supports constraint for PID but not for also using thermometers/affect as additional zero-point identifications
** But that is for a different concept of Pure Independent (can evaluate party therms differently), constraints basically say pure independents are really neutral

test _b[/attpartythermdiff:0.post#c.cut4]+_b[/attpartythermdiff:0.post#c.cut5]=0
test _b[/attpartythermdiff:1.post#c.cut4]+_b[/attpartythermdiff:1.post#c.cut5]=0, accum

test _b[/partyaffect:0.post#c.cut2]+_b[/partyaffect:0.post#c.cut3]=0
test _b[/partyaffect:1.post#c.cut2]+_b[/partyaffect:1.post#c.cut3]=0, accum

***********************************************************************
** Balancing party strength within group isn't that costly either

test  _b[/strongpartisan:0.post#c.cut1]=_b[/strongpartisan:1.post#c.cut1]
test  _b[/strongpartisan:1.post#c.cut1]+_b[/strongpartisan:1.post#c.cut2]=0
test  _b[/strongpartisan:0.post#c.cut1]+_b[/strongpartisan:0.post#c.cut2]=0, accum

constraint 10 _b[/strongpartisan:1.post#c.cut1]+_b[/strongpartisan:1.post#c.cut2]=0
constraint 11 _b[/strongpartisan:0.post#c.cut1]+_b[/strongpartisan:0.post#c.cut2]=0
constraint 12 _b[/strongpartisan:0.post#c.cut2]=_b[/strongpartisan:1.post#c.cut2]=0

***********************************************************************
*******************************************************************
** Published estimates use balancing model that allow balance to change pre/post
********************************************************************
** Table 2, Varying Relevance
*****************************************************************************

gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm constraints(1/3 4/5 7/8 10/11) ginvariant(none) from(newbeta)
test _b[/var(LPID)#1.post]= _b[/var(LPID)#0.post]
est save prepost_auggrm_free, replace
est store free


** Compare difficulty parameters
** difficulty is retrieved from cut-point divided by slope

**** FIGURE 3

nlcom _b[/partyid_base:0.post#c.cut1]/_b[partyid_base:0.post#c.LPID]
nlcom _b[/partyid_base:0.post#c.cut2]/_b[partyid_base:0.post#c.LPID]


** Pre

nlcom _b[/attpartythermdiff:0.post#c.cut4]/_b[attpartythermdiff:0.post#c.LPID]
nlcom _b[/attpartythermdiff:0.post#c.cut5]/_b[attpartythermdiff:0.post#c.LPID]

nlcom _b[/partyaffect:0.post#c.cut2]/_b[partyaffect:0.post#c.LPID]
nlcom _b[/partyaffect:0.post#c.cut3]/_b[partyaffect:0.post#c.LPID]

nlcom _b[/strongpartisan:0.post#c.cut1]/_b[strongpartisan:0.post#c.LPID]
nlcom _b[/strongpartisan:0.post#c.cut2]/_b[strongpartisan:0.post#c.LPID]

** Post

nlcom _b[/attpartythermdiff:1.post#c.cut4]/_b[attpartythermdiff:1.post#c.LPID]
nlcom _b[/attpartythermdiff:1.post#c.cut5]/_b[attpartythermdiff:1.post#c.LPID]

nlcom _b[/partyaffect:1.post#c.cut2]/_b[partyaffect:1.post#c.LPID]
nlcom _b[/partyaffect:1.post#c.cut3]/_b[partyaffect:1.post#c.LPID]

nlcom _b[/strongpartisan:1.post#c.cut1]/_b[strongpartisan:1.post#c.LPID]
nlcom _b[/strongpartisan:1.post#c.cut2]/_b[strongpartisan:1.post#c.LPID]



test _b[/var(LPID)#1.post]= _b[/var(LPID)#0.post]
estimates replay, byparm

predict theta_grm_free if e(sample), latent intpoints(7)
gen partystrength_free = abs(theta_grm_free)


*****************************************************
*** DIFFICULTY PARAMETERS FOR SI Figure C.2
****************************************************               
matrix difficulty = J(4,28,0)
matrix variance = J(4,28,0)

nlcom _b[/partyid_base:0.post#c.cut1]/1
matrix difficulty[1,1] = r(b)
matrix variance[1,1] = r(V)
nlcom _b[/partyid_base:0.post#c.cut2]/1
matrix difficulty[1,2] = r(b)
matrix variance[1,2] = r(V)
                      
nlcom _b[/attpresthermdiff:0.post#c.cut1]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[1,3] = r(b)
matrix variance[1,3] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut2]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[1,4] = r(b)
matrix variance[1,4] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut3]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[1,5] = r(b)
matrix variance[1,5] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut4]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[1,6] = r(b)
matrix variance[1,6] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut5]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[1,7] = r(b)
matrix variance[1,7] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut6]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[1,8] = r(b)
matrix variance[1,8] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut7]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[1,9] = r(b)
matrix variance[1,9] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut8]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[1,10] = r(b)
matrix variance[1,10] = r(V)

nlcom _b[/attpartythermdiff:0.post#c.cut1]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[1,11] = r(b)
matrix variance[1,11] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut2]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[1,12] = r(b)
matrix variance[1,12] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut3]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[1,13] = r(b)
matrix variance[1,13] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut4]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[1,14] = r(b)
matrix variance[1,14] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut5]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[1,15] = r(b)
matrix variance[1,15] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut6]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[1,16] = r(b)
matrix variance[1,16] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut7]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[1,17] = r(b)
matrix variance[1,17] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut8]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[1,18] = r(b)
matrix variance[1,18] = r(V)

nlcom _b[/partyaffect:0.post#c.cut1]/_b[partyaffect:0.post#c.LPID]
matrix difficulty[1,19] = r(b)
matrix variance[1,19] = r(V)
nlcom _b[/partyaffect:0.post#c.cut2]/_b[partyaffect:0.post#c.LPID]
matrix difficulty[1,20] = r(b)
matrix variance[1,20] = r(V)
nlcom _b[/partyaffect:0.post#c.cut3]/_b[partyaffect:0.post#c.LPID]
matrix difficulty[1,21] = r(b)
matrix variance[1,21] = r(V)
nlcom _b[/partyaffect:0.post#c.cut4]/_b[partyaffect:0.post#c.LPID]
matrix difficulty[1,22] = r(b)
matrix variance[1,22] = r(V)

nlcom _b[/partyprespref:0.post#c.cut1]/_b[partyprespref:0.post#c.LPID]
matrix difficulty[1,23] = r(b)
matrix variance[1,23] = r(V)
nlcom _b[/partyprespref:0.post#c.cut2]/_b[partyprespref:0.post#c.LPID]
matrix difficulty[1,24] = r(b)
matrix variance[1,24] = r(V)

nlcom _b[/partyvotehouse3:0.post#c.cut1]/_b[partyvotehouse3:0.post#c.LPID]
matrix difficulty[1,25] = r(b)
matrix variance[1,25] = r(V)
nlcom _b[/partyvotehouse3:0.post#c.cut2]/_b[partyvotehouse3:0.post#c.LPID]
matrix difficulty[1,26] = r(b)
matrix variance[1,26] = r(V)

nlcom _b[/strongpartisan:0.post#c.cut1]/_b[strongpartisan:0.post#c.LPID]
matrix difficulty[1,27] = r(b)
matrix variance[1,27] = r(V)
nlcom _b[/strongpartisan:0.post#c.cut2]/_b[strongpartisan:0.post#c.LPID]
matrix difficulty[1,28] = r(b)
matrix variance[1,28] = r(V)

***

nlcom _b[/partyid_base:1.post#c.cut1]/1
matrix difficulty[2,1] = r(b)
matrix variance[2,1] = r(V)
nlcom _b[/partyid_base:1.post#c.cut2]/1
matrix difficulty[2,2] = r(b)
matrix variance[2,2] = r(V)
                      
nlcom _b[/attpresthermdiff:1.post#c.cut1]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[2,3] = r(b)
matrix variance[2,3] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut2]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[2,4] = r(b)
matrix variance[2,4] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut3]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[2,5] = r(b)
matrix variance[2,5] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut4]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[2,6] = r(b)
matrix variance[2,6] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut5]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[2,7] = r(b)
matrix variance[2,7] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut6]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[2,8] = r(b)
matrix variance[2,8] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut7]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[2,9] = r(b)
matrix variance[2,9] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut8]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[2,10] = r(b)
matrix variance[2,10] = r(V)

nlcom _b[/attpartythermdiff:1.post#c.cut1]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[2,11] = r(b)
matrix variance[2,11] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut2]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[2,12] = r(b)
matrix variance[2,12] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut3]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[2,13] = r(b)
matrix variance[2,13] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut4]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[2,14] = r(b)
matrix variance[2,14] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut5]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[2,15] = r(b)
matrix variance[2,15] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut6]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[2,16] = r(b)
matrix variance[2,16] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut7]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[2,17] = r(b)
matrix variance[2,17] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut8]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[2,18] = r(b)
matrix variance[2,18] = r(V)

nlcom _b[/partyaffect:1.post#c.cut1]/_b[partyaffect:1.post#c.LPID]
matrix difficulty[2,19] = r(b)
matrix variance[2,19] = r(V)
nlcom _b[/partyaffect:1.post#c.cut2]/_b[partyaffect:1.post#c.LPID]
matrix difficulty[2,20] = r(b)
matrix variance[2,20] = r(V)
nlcom _b[/partyaffect:1.post#c.cut3]/_b[partyaffect:1.post#c.LPID]
matrix difficulty[2,21] = r(b)
matrix variance[2,21] = r(V)
nlcom _b[/partyaffect:1.post#c.cut4]/_b[partyaffect:1.post#c.LPID]
matrix difficulty[2,22] = r(b)
matrix variance[2,22] = r(V)

nlcom _b[/partyprespref:1.post#c.cut1]/_b[partyprespref:1.post#c.LPID]
matrix difficulty[2,23] = r(b)
matrix variance[2,23] = r(V)
nlcom _b[/partyprespref:1.post#c.cut2]/_b[partyprespref:1.post#c.LPID]
matrix difficulty[2,24] = r(b)
matrix variance[2,24] = r(V)

nlcom _b[/partyvotehouse3:1.post#c.cut1]/_b[partyvotehouse3:1.post#c.LPID]
matrix difficulty[2,25] = r(b)
matrix variance[2,25] = r(V)
nlcom _b[/partyvotehouse3:1.post#c.cut2]/_b[partyvotehouse3:1.post#c.LPID]
matrix difficulty[2,26] = r(b)
matrix variance[2,26] = r(V)

nlcom _b[/strongpartisan:1.post#c.cut1]/_b[strongpartisan:1.post#c.LPID]
matrix difficulty[2,27] = r(b)
matrix variance[2,27] = r(V)
nlcom _b[/strongpartisan:1.post#c.cut2]/_b[strongpartisan:1.post#c.LPID]
matrix difficulty[2,28] = r(b)
matrix variance[2,28] = r(V)


*** Go back to fixed model with same constraints
gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm constraints(1/12) from(newbeta)
est save prepost_auggrm_fixed, replace
est store fixed

nlcom _b[/partyid_base:0.post#c.cut1]/1
matrix difficulty[3,1] = r(b)
matrix variance[3,1] = r(V)
nlcom _b[/partyid_base:0.post#c.cut2]/1
matrix difficulty[3,2] = r(b)
matrix variance[3,2] = r(V)
                      
nlcom _b[/attpresthermdiff:0.post#c.cut1]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[3,3] = r(b)
matrix variance[3,3] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut2]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[3,4] = r(b)
matrix variance[3,4] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut3]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[3,5] = r(b)
matrix variance[3,5] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut4]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[3,6] = r(b)
matrix variance[3,6] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut5]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[3,7] = r(b)
matrix variance[3,7] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut6]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[3,8] = r(b)
matrix variance[3,8] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut7]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[3,9] = r(b)
matrix variance[3,9] = r(V)
nlcom _b[/attpresthermdiff:0.post#c.cut8]/_b[attpresthermdiff:0.post#c.LPID]
matrix difficulty[3,10] = r(b)
matrix variance[3,10] = r(V)

nlcom _b[/attpartythermdiff:0.post#c.cut1]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[3,11] = r(b)
matrix variance[3,11] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut2]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[3,12] = r(b)
matrix variance[3,12] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut3]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[3,13] = r(b)
matrix variance[3,13] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut4]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[3,14] = r(b)
matrix variance[3,14] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut5]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[3,15] = r(b)
matrix variance[3,15] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut6]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[3,16] = r(b)
matrix variance[3,16] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut7]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[3,17] = r(b)
matrix variance[3,17] = r(V)
nlcom _b[/attpartythermdiff:0.post#c.cut8]/_b[attpartythermdiff:0.post#c.LPID]
matrix difficulty[3,18] = r(b)
matrix variance[3,18] = r(V)

nlcom _b[/partyaffect:0.post#c.cut1]/_b[partyaffect:0.post#c.LPID]
matrix difficulty[3,19] = r(b)
matrix variance[3,19] = r(V)
nlcom _b[/partyaffect:0.post#c.cut2]/_b[partyaffect:0.post#c.LPID]
matrix difficulty[3,20] = r(b)
matrix variance[3,20] = r(V)
nlcom _b[/partyaffect:0.post#c.cut3]/_b[partyaffect:0.post#c.LPID]
matrix difficulty[3,21] = r(b)
matrix variance[3,21] = r(V)
nlcom _b[/partyaffect:0.post#c.cut4]/_b[partyaffect:0.post#c.LPID]
matrix difficulty[3,22] = r(b)
matrix variance[3,22] = r(V)

nlcom _b[/partyprespref:0.post#c.cut1]/_b[partyprespref:0.post#c.LPID]
matrix difficulty[3,23] = r(b)
matrix variance[3,23] = r(V)
nlcom _b[/partyprespref:0.post#c.cut2]/_b[partyprespref:0.post#c.LPID]
matrix difficulty[3,24] = r(b)
matrix variance[3,24] = r(V)

nlcom _b[/partyvotehouse3:0.post#c.cut1]/_b[partyvotehouse3:0.post#c.LPID]
matrix difficulty[3,25] = r(b)
matrix variance[3,25] = r(V)
nlcom _b[/partyvotehouse3:0.post#c.cut2]/_b[partyvotehouse3:0.post#c.LPID]
matrix difficulty[3,26] = r(b)
matrix variance[3,26] = r(V)

nlcom _b[/strongpartisan:0.post#c.cut1]/_b[strongpartisan:0.post#c.LPID]
matrix difficulty[3,27] = r(b)
matrix variance[3,27] = r(V)
nlcom _b[/strongpartisan:0.post#c.cut2]/_b[strongpartisan:0.post#c.LPID]
matrix difficulty[3,28] = r(b)
matrix variance[3,28] = r(V)

***

nlcom _b[/partyid_base:1.post#c.cut1]/1
matrix difficulty[4,1] = r(b)
matrix variance[4,1] = r(V)
nlcom _b[/partyid_base:1.post#c.cut2]/1
matrix difficulty[4,2] = r(b)
matrix variance[4,2] = r(V)
                      
nlcom _b[/attpresthermdiff:1.post#c.cut1]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[4,3] = r(b)
matrix variance[4,3] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut2]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[4,4] = r(b)
matrix variance[4,4] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut3]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[4,5] = r(b)
matrix variance[4,5] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut4]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[4,6] = r(b)
matrix variance[4,6] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut5]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[4,7] = r(b)
matrix variance[4,7] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut6]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[4,8] = r(b)
matrix variance[4,8] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut7]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[4,9] = r(b)
matrix variance[4,9] = r(V)
nlcom _b[/attpresthermdiff:1.post#c.cut8]/_b[attpresthermdiff:1.post#c.LPID]
matrix difficulty[4,10] = r(b)
matrix variance[4,10] = r(V)

nlcom _b[/attpartythermdiff:1.post#c.cut1]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[4,11] = r(b)
matrix variance[4,11] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut2]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[4,12] = r(b)
matrix variance[4,12] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut3]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[4,13] = r(b)
matrix variance[4,13] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut4]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[4,14] = r(b)
matrix variance[4,14] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut5]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[4,15] = r(b)
matrix variance[4,15] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut6]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[4,16] = r(b)
matrix variance[4,16] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut7]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[4,17] = r(b)
matrix variance[4,17] = r(V)
nlcom _b[/attpartythermdiff:1.post#c.cut8]/_b[attpartythermdiff:1.post#c.LPID]
matrix difficulty[4,18] = r(b)
matrix variance[4,18] = r(V)

nlcom _b[/partyaffect:1.post#c.cut1]/_b[partyaffect:1.post#c.LPID]
matrix difficulty[4,19] = r(b)
matrix variance[4,19] = r(V)
nlcom _b[/partyaffect:1.post#c.cut2]/_b[partyaffect:1.post#c.LPID]
matrix difficulty[4,20] = r(b)
matrix variance[4,20] = r(V)
nlcom _b[/partyaffect:1.post#c.cut3]/_b[partyaffect:1.post#c.LPID]
matrix difficulty[4,21] = r(b)
matrix variance[4,21] = r(V)
nlcom _b[/partyaffect:1.post#c.cut4]/_b[partyaffect:1.post#c.LPID]
matrix difficulty[4,22] = r(b)
matrix variance[4,22] = r(V)

nlcom _b[/partyprespref:1.post#c.cut1]/_b[partyprespref:1.post#c.LPID]
matrix difficulty[4,23] = r(b)
matrix variance[4,23] = r(V)
nlcom _b[/partyprespref:1.post#c.cut2]/_b[partyprespref:1.post#c.LPID]
matrix difficulty[4,24] = r(b)
matrix variance[4,24] = r(V)

nlcom _b[/partyvotehouse3:1.post#c.cut1]/_b[partyvotehouse3:1.post#c.LPID]
matrix difficulty[4,25] = r(b)
matrix variance[4,25] = r(V)
nlcom _b[/partyvotehouse3:1.post#c.cut2]/_b[partyvotehouse3:1.post#c.LPID]
matrix difficulty[4,26] = r(b)
matrix variance[4,26] = r(V)

nlcom _b[/strongpartisan:1.post#c.cut1]/_b[strongpartisan:1.post#c.LPID]
matrix difficulty[4,27] = r(b)
matrix variance[4,27] = r(V)
nlcom _b[/strongpartisan:1.post#c.cut2]/_b[strongpartisan:1.post#c.LPID]
matrix difficulty[4,28] = r(b)
matrix variance[4,28] = r(V)

predict theta_grm_fixed if e(sample), latent intpoints(7)
gen partystrength_fixed = abs(theta_grm_fixed)

****************************************
***  Table 2, average strength means, top row
****************************************

mean partystrength_fixed partystrength_free [pw=newweight], over(post)

estout fixed free, cells(b(fmt(a4)) se(fmt(3) par)) stats(N ll)


*********************************
** Now add issues, Table C.4 Estimates
*********************************

gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan partylibcon3 partyabortion partygovserv3 partyaidtoblacks <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  byparm

matrix beta = e(b)

gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan partylibcon3 partyabortion partygovserv3 partyaidtoblacks <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm constraints(1/12) from(beta)
est store fixed
predict theta_grm_fixed_issues if e(sample), latent intpoints(7)
gen partystrength_fixed_issues = abs(theta_grm_fixed_issues)

matrix beta = e(b)

gsem (partyid_base <- LPID@1, ologit ) (attpresthermdiff attpartythermdiff partyaffect partyprespref partyvotehouse3 strongpartisan partylibcon3 partyabortion partygovserv3 partyaidtoblacks <- LPID, ologit) [pw=newweight], group(post) intpoints(15)  mean(0: LPID@MU1) mean(1: LPID@MU2) byparm  constraints(1/3 4/5 7/8 10/11) ginvariant(none) from(beta)
est store free
predict theta_grm_free_issues if e(sample), latent intpoints(7)
gen partystrength_free_issues = abs(theta_grm_free_issues)

mean partystrength_fixed_issues partystrength_free_issues [pw=newweight], over(post)

estout fixed free, cells(b(fmt(a4)) se(fmt(3) par)) stats(N ll)

svmat difficulty, names(DIFF)
svmat variance, names(DIFFVAR)

saveold "data/gsem_grm_prepost_1952.dta", version(12) replace

clear

